﻿
@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link href="~/layui-v2.6.8/layui-v2.6.8/layui/css/layui.css" rel="stylesheet" />

    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

    <div class="demoTable">
        查询：
        <div class="layui-inline">
            <input class="layui-input" name="userName" id="demoReload" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
        <button class="layui-btn" data-type="add">添加</button>
    </div>
    <div class="layui-row">
        <div class="layui-col-xs2">

            <div id="test1" class="demo-tree demo-tree-box"></div>
        </div>
        <div class="layui-col-xs10">
            <table class="layui-hide" id="test" lay-filter="test"></table>
        </div>
    </div>

    <div>

        <div>

            <script type="text/html" id="switchTpl">

                <!-- 这里的 checked 的状态只是演示 -->
                <input type="checkbox" name="status" value="{{d.status}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="sexDemo" {{ d.status == 1 ? 'checked' : '' }}>

            </script>

            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="ShuShow">设置权限</a>
                <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="updatepwd">重置密码</a>
            </script>
        </div>
    </div>

    <script src="~/lib/jquery/dist/jquery.js"></script>
    <script src="~/layui-v2.6.8/layui-v2.6.8/layui/layui.js"></script>
    @*<script src="//res.layui.com/layui/dist/layui.js" charset="utf-8"></script>*@
    <!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

    <script>

        //（4） 前端：浏览器请求服务器的时候，要带上JWT
        $.ajaxSetup({
            beforeSend: function (xhr) {
                if (localStorage.getItem("token") !== null) {
                    //（4） 前端：浏览器请求服务器的时候，要带上JWT
                    xhr.setRequestHeader('Authorization', 'Bearer ' + localStorage.getItem("token"));
                }
            }
        });

        layui.use(['table', 'tree', 'util'], function () {
            var table = layui.table;
            var tree = layui.tree
                , layer = layui.layer
                , util = layui.util

            var Hob = table.render({
                elem: '#test'
                , url: 'https://localhost:44398/api/UsersAPI/GetShow'
                , title: '用户数据表'
                , cols: [[
                    { type: 'checkbox', fixed: 'left' }
                    , { field: 'userCode', title: '编号', width: 120, fixed: 'left', unresize: true, sort: true }
                    , { field: 'userName', title: '姓名', width: 120, edit: 'text' }
                    , { field: 'loginName', title: '登录名', width: 80, edit: 'text', sort: true }
                    , { field: 'deptName', title: '所属', width: 100 }
                    , { field: 'status', title: '状态', width: 100, templet: '#switchTpl', unresize: true }
                    , { field: 'createDate', title: '加入时间', width: 200 }
                    , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 270 }
                ]]
                , page: true
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                //console.log(obj)
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: 'https://localhost:44398/api/UsersAPI/Delete?id=' + data.userID,
                            type: 'post',
                            success: function (d) {
                                layer.msg("删除成功!", { time: 1500, icon: 1 }, function () {
                                    Hob.reload();
                                })
                            }
                        })
                    });
                } else if (obj.event === 'edit') {
                    layer.open({
                        type: 2,
                        area: ['700px', '550px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/UsersWeb/Fill?id=' + data.userID,
                    });
                } else if (obj.event === 'updatepwd') {
                    //询问框
                    layer.confirm('确定修改为123456吗？', {
                        btn: ['确定', '取消'] //按钮
                    }, function () {
                        $.ajax({
                            url: 'https://localhost:44398/api/UsersAPI/UpdatePwd?userID=' + data.userID,
                            type: 'post',
                            success: function (d) {
                                layer.msg('修改成功', { icon: 1 });
                                Hob.reload();
                            }
                        })
                    }, function () {
                        layer.msg('停止修改', {
                            time: 20000, //20s后自动关闭
                            btn: ['明白了', '知道了']
                        });
                    });


                } else if (obj.event === 'ShuShow') {
                    layer.open({
                        type: 2,
                        area: ['700px', '500px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/UsersWeb/ShuShow?userId=' + data.userID,
                    });
                }
            });

            var $ = layui.$, active = { //查询
                reload: function () {
                    var demoReload = $('#demoReload');

                    //执行重载
                    table.reload('test', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {
                            userName: demoReload.val()
                        }
                    });
                },
                add: function () {  //添加的弹框
                    layer.open({
                        type: 2,
                        area: ['700px', '550px'],
                        fixed: false, //不固定
                        maxmin: true,
                        content: '/UsersWeb/Create'
                    });
                }
            };

            $('.demoTable .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            //文档就绪
            $(function () {
                $.ajax({
                    url: "https://localhost:44398/api/UsersAPI/Shu",
                    type: "get",
                    success: function (data1) {
                        //常规用法

                        //基本演示
                        tree.render({
                            elem: '#test1'
                            , data: data1
                            , showCheckbox: false //是否显示复选框
                            , id: 'demoId1'
                            , isJump: true //是否允许点击节点时弹出新窗口跳转
                            , click: function (obj) {
                                var data = obj.data;  //获取当前点击的节点数据
                                console.log(data);

                                table.reload('test', {
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                    , where: {
                                        parentID: data.id
                                    }
                                });


                            }
                        });
                    }
                })
            })

        });

    </script>

</body>
</html>
